import {_decorator, native, sys} from "cc";
import {EncodeUtil} from "../utils/EncodeUtil";

const {ccclass, property} = _decorator;

@ccclass("StorageManager")
export class StorageManager {

    /**
     *  持久化数据
     */
    private readonly _persistenceData: { [key: string]: any } = {};
    /**
     * 持久化路径
     */
    private readonly _path: any = null;
    private KEY_CONFIG: string = 'template';

    constructor() {
        // 设置当前持久化数据
        this._persistenceData = {};
        // 读取文件存储路径路径
        this._path = this._getConfigPath();
        // 读取内容
        let content = sys.isNative ? native.fileUtils.getValueMapFromFile(this._path)[this.KEY_CONFIG] : sys.localStorage.getItem(this.KEY_CONFIG);
        // 存在内容
        if (!content) {
            return;
        }

        try {
            // 解密
            content = EncodeUtil.decode(content);
            //初始化操作
            this._persistenceData = JSON.parse(content);
        } catch (exception) {
            // 读取数据异常
        }
    }

    /**
     * 获取配置文件路径
     * @returns 获取配置文件路径
     */
    private _getConfigPath() {
        // 获取平台
        let platform: any = sys.platform;
        // 构建路径
        let path: string = "";
        // WINDOWS
        if (platform === sys.OS.WINDOWS) {
            path = "src/conf";
        } else if (platform === sys.OS.LINUX) {
            path = "./conf";
        } else {
            if (sys.isNative) {
                path = native.fileUtils.getWritablePath();
                path = path + "conf";
            } else {
                path = "src/conf";
            }
        }

        return path;
    }

    /**
     * 保存配置文件
     * @returns
     */
    private persistenceData() {
        // 写入文件
        let zipStr = EncodeUtil.encode(JSON.stringify(this._persistenceData));
        if (!sys.isNative) {
            const ls = sys.localStorage;
            ls.setItem(this.KEY_CONFIG, zipStr);
            return;
        }

        const valueObj: any = {};
        valueObj[this.KEY_CONFIG] = zipStr;
        native.fileUtils.writeValueMapToFile(valueObj, this._path);
    }

    /**
     * 设置全局数据{key:value}
     * @param {string} key 关键字
     * @param {any}value  存储值
     * @returns
     */
    public setGlobalData(key: string, value: any) {
        this._persistenceData[key] = value;
        this.persistenceData();
    }

    /**
     * 获取全局数据{key:value}
     * @param {string} key 关键字
     * @returns
     */
    public getGlobalData(key: string) {
        return this._persistenceData[key];
    }

    /**
     * 设置私有数据{owner:{key:value}}
     * @param {string} owner 持有者
     * @param {string} key 关键字
     * @param {any}value  存储值
     * @returns
     */
    public setPrivateData(owner: string, key: string, value: any) {
        if (!this._persistenceData[owner]) {
            this._persistenceData[owner] = {}
        }

        this._persistenceData[owner][key] = value;
        this.persistenceData();
    }

    /**
     * 获取私有数据{owner:{key:value}}
     * @param {string} owner 持有者
     * @param {string} key 关键字
     * @returns
     */
    public getPrivateData(owner: string, key: string) {
        return this._persistenceData[owner] ? this._persistenceData[owner][key] : null;
    }

}
